package com.zq.qq2.thread;

import android.content.Context;
import android.os.Handler;
import android.widget.Toast;

import com.zq.qq2.Constant;
import com.zq.qq2.FileUtils;
import com.zq.qq2.MediaUtil;
import com.zq.qq2.R;
import com.zq.qq2.libsupersu.RootUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FilenameFilter;

/**
 * Created by xuebiao on 16/4/6.
 */
public class WeixinStartup extends Thread {
    Context context;
    Handler handler;
    JSONObject json;
    public WeixinStartup(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }
    @Override
    public void run() {
        // 读取微信账号
        String db = FileUtils.read("/storage/sdcard0/zhangqing/shouji.txt");
        json = FileUtils.GetJson(db);

        boolean suportWeixin = FileUtils.GetBoolean(json, "suportWeixin", false);

        if ( !suportWeixin ) {
            // 读取微信账号
            String wx = FileUtils.read("/mnt/sdcard/zhangqing/src/weixin.txt");
            if ( wx.equals("") ) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "没有检测到微信账号", Toast.LENGTH_LONG).show();
                    }
                });
                MediaUtil.Vibrater(context, R.raw.weixinempty, 5);
                return;
            }
            String[] wxa = wx.split("\n");
            try {
                String[] ac = wxa[0].split("----");
                json.put("phone_line1number", "+86" + ac[0].trim());
                json.put("weixinPasswd", ac[1].trim());
                json.put("suportWeixin", true);
                FileUtils.write("/data/local/db.json", json.toString());
            } catch (JSONException e) {
            }
            StringBuilder sb = new StringBuilder();
            for( int i = 1; i < wxa.length; i ++ ) {
                sb.append(wxa[i] + "\n");
            }
            FileUtils.write("/mnt/sdcard/zhangqing/src/weixin.txt", sb.toString().trim());

            File fs = new File("/mnt/sdcard/zhangqing/data/");
            if ( fs.exists() ) {
                File[] data = new File("/mnt/sdcard/zhangqing/data/").listFiles();
                for (File fd : data) {
                    File[] files = fd.listFiles(new FilenameFilter() {
                        @Override
                        public boolean accept(File dir, String filename) {
                            return filename.endsWith(Constant.getDeviceId(context) + ".txt");
                        }
                    });
                    for (File f : files) {
                        try {
                            FileUtils.write(f.getAbsolutePath(), json.toString());
                            Constant.Debug("更新数据文件:" + f.getAbsolutePath());
                        } catch (Exception e) {

                        }
                    }
                }
            }
        }

        RootUtil.Get().BashExecute("am start -n com.tencent.mm/.ui.LauncherUI");
        int count = 0;
        do {
            try {
                sleep(1000);
            } catch (InterruptedException e) {

            }
            if ( count ++ > 60 )
                break;
            RootUtil.Get().BashExecute("input tap 123 900");
        } while(true);
        RootUtil.Get().BashExecute("input tap 220 320");
        RootUtil.Get().BashExecute("input text " + FileUtils.GetString(json, "weixinPasswd"));
        RootUtil.Get().BashExecute("input tap 260 418");
        count = 0;
        do {
            try {
                sleep(3000);
            } catch (InterruptedException e) {

            }
            if ( count ++ > 60 )
                break;
            RootUtil.Get().BashExecute("input tap 315 590");

        } while(true);
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, "登陆成功!", Toast.LENGTH_SHORT).show();
                MediaUtil.Notify(context, R.raw.weixinsuccess);
            }
        });
    }
}
